    
package com.mycjj.android;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

import android.app.Activity;


public class ActivityManager {
	private static ActivityManager instance;
	private static Map<String,Stack<Activity>> stackMap;
	private ActivityManager(){
		
	}
	
	public static ActivityManager getInstance(){
		if(instance == null){
			instance = new ActivityManager();
			stackMap = new HashMap<String,Stack<Activity>>();
		}
		return instance;
	}
	
	public void popActivity(String key){
		Stack<Activity> currentStack = stackMap.get(key);
		if(currentStack != null){
			Activity activity = currentStack.lastElement();
			if(activity != null){
				activity.finish();
				activity = null;
			}
		}
	}
	
	public void popActivity(String key,Activity act){
		if(act != null){
			Stack<Activity> currentStack = stackMap.get(key);
			if(currentStack != null){
				currentStack.remove(act);
			}
			act.finish();
			act = null;
		}
	}
	
	public void popAllByKey(String key){
		Stack<Activity> currentStack = stackMap.get(key);
		if(currentStack != null){
			for(Activity act : currentStack){
				if(act != null){
					act.finish();
					act = null;
				}
			}
		}
	}
	
	public void pushActivity(String key,Activity activity){
		Stack<Activity> currentStack = stackMap.get(key);
		if(currentStack == null){
			currentStack = new Stack<Activity>();
		}
		currentStack.add(activity);
		stackMap.put(key, currentStack);
	}
}
